package com.ocean.courtyard.core.datasource.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.datasource.DataSourceFactory;

import javax.sql.DataSource;
import java.util.Properties;

public class HikariDataSourceFactory implements DataSourceFactory {

    private final HikariConfig config = new HikariConfig();

    @Override
    public void setProperties(Properties properties) {
        config.setJdbcUrl(properties.getProperty("jdbcUrl"));
        config.setUsername(properties.getProperty("username"));
        config.setPassword(properties.getProperty("password"));
        config.setMaximumPoolSize(Integer.parseInt(properties.getProperty("maximumPoolSize")));
        config.setMinimumIdle(Integer.parseInt(properties.getProperty("minimumIdle")));
        config.setIdleTimeout(Long.parseLong(properties.getProperty("idleTimeout")));
        config.setMaxLifetime(Long.parseLong(properties.getProperty("maxLifetime")));
        config.setConnectionTimeout(Long.parseLong(properties.getProperty("connectionTimeout")));
    }

    @Override
    public DataSource getDataSource() {
        return new HikariDataSource(config);
    }
}